package cn.iocoder.yudao.module.pay.transfer.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.api.pay.transfer.dto.PayTransferCreateDTO;
import cn.iocoder.yudao.module.api.pay.transfer.enums.UnifiedTransferStatusEnum;
import cn.iocoder.yudao.module.pay.transfer.convert.PayTransferConvert;
import cn.iocoder.yudao.module.pay.transfer.dal.dataobject.PayTransferDO;
import cn.iocoder.yudao.module.pay.transfer.service.PayTransferService;
import cn.iocoder.yudao.module.pay.transfer.vo.PayTransferCreateRespVO;
import cn.iocoder.yudao.module.pay.transfer.vo.PayTransferPageItemRespVO;
import cn.iocoder.yudao.module.pay.transfer.vo.PayTransferPageReqVO;
import cn.iocoder.yudao.module.pay.transfer.vo.PayTransferRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 转账单")
@RestController
@RequestMapping("/pay/transfer")
@Validated
public class PayTransferController {

    @Resource
    private PayTransferService payTransferService;

    @PostMapping("/create")
    @Operation(summary = "创建转账单，发起转账")
    @PreAuthorize("@ss.hasPermission('pay:transfer:create')")
    public CommonResult<PayTransferCreateRespVO> createPayTransfer(@Valid @RequestBody PayTransferCreateDTO dto) {
        String no = payTransferService.createTransfer(dto);
        PayTransferCreateRespVO respVO = new PayTransferCreateRespVO();
        respVO.setNo(no);
        respVO.setStatus(UnifiedTransferStatusEnum.WAITING.getValue());
        return success(respVO);
    }

    @GetMapping("/get")
    @Operation(summary = "获得转账订单")
    @PreAuthorize("@ss.hasPermission('pay:transfer:query')")
    public CommonResult<PayTransferRespVO> getTransfer(@RequestParam("id") Long id) {
        return success(PayTransferConvert.INSTANCE.convert(payTransferService.getTransfer(id)));
    }

    @GetMapping("/page")
    @Operation(summary = "获得转账订单分页")
    @PreAuthorize("@ss.hasPermission('pay:transfer:query')")
    public CommonResult<PageResult<PayTransferPageItemRespVO>> getTransferPage(@Valid PayTransferPageReqVO pageVO) {
        PageResult<PayTransferDO> pageResult = payTransferService.getTransferPage(pageVO);
        return success(PayTransferConvert.INSTANCE.convertPage(pageResult));
    }
}
